<?php


 /**
  * 注册用户竞标
  * auction.js  bid
  * Enter description here ...
  * @var unknown_type
  */

session_start();
define('ROOT_PATH', str_replace("\\", '/', dirname(__FILE__)));
$config = include(ROOT_PATH.'/Lib/common.ini.php');


	$data = Init_GP(array('id'));   //商品竞拍ID
	$id = (int)($data['id']);
	
	$userdata = $_SESSION['user'];   //当前用户
	$sql = "SELECT * FROM `{$prefix}user` where `id`={$userdata['id']}";
	$userdata = $db->fetch_first($sql);
	if ($userdata == false){
		$msg = 2;  //请先登录	
	}else {
		$auctiondata = $get->getAuctionData($id);
		
		if (empty($auctiondata)){
				$msg = 7; //竞拍不存在
		}else {
			if ($auctiondata['state'] == 0){
				$msg = 8;  //竞拍未开始
			}elseif ($auctiontmp['state'] >= 2){
				$msg = 0; //竞拍已结束
			}elseif($auctiondata['type'] == 1 && $userdata['old'] == 1){
				$msg = 10;//新人竞拍
			}else {
				$bool = true;
				
				
				//TODO:用户竞标次数上限  每个商品每个用户竞标次数上限值
				if(!empty($auctiondata['boutlimit'])){
					$sql = "SELECT count(*) as count FROM `{$prefix}auction_log` where `aid` = {$auctiondata['id']} and `uid` = {$userdata['id']}";
					$bout = $db->fetch_first($sql);
					$bout = $bout['count'];
					if($bout >= $auctiondata['boutlimit']){
						$bool = false;
						$msg = 15;
					}
				}
				
				
				if($bool){
					//用户参与本商品竞标情况
					$sql = "SELECT * FROM `{$prefix}participate` where `aid`={$auctiondata['id']} and `uid`={$userdata['id']}";
					$participate = $db->fetch_first($sql);
					if (empty($participate)){
						 //用户对本商品竞标情况记入
						$sql = "INSERT INTO `{$prefix}participate` (`id`, `uid`, `aid`) VALUES (NULL, '{$userdata['id']}', '{$auctiondata['id']}')";
						$db->query($sql);
					}			
					
					
					if ($auctiondata['uid'] == $userdata['id']){  //最后竞拍者是自己
						$msg = 4; 
					}else {
						
						//TODO:。。。。
						if (!empty($auctiondata['isdiscount'])){
							if(!empty($auctiondata['beforebout']) && !empty($auctiondata['discount'])){
								if($auctiondata['auctioncount'] <= $auctiondata['beforebout']){
									$auctiondata['usebeat'] = $auctiondata['usebeat'] * $auctiondata['discount'] / 10;
								}
							}
						}
						
						
						
						
						if ($userdata['priority'] == 1){  //优先使用实体币
							if ($userdata['beat'] < $auctiondata['usebeat']){    //用户实体币  < 竞标一次所需币数
								if ($userdata['pointb'] < $auctiondata['usebeat']){ //虚拟竞标币 < 竞标一次所需币数
									//实体币数和虚拟币数均不够竞拍一次所需竞标币数
									$msg = 3; //余额不足
								}else { 
									//执行虚拟币竞拍操作
									usepointb($userdata,$auctiondata);      //实体币数不够虚拟币数够，采用虚拟币竞拍
									addpointc($userdata,$auctiondata);   //增加积分
									$msg = 1;
								}	
							}else {  //执行实体币竞拍
								usebeat($userdata,$auctiondata);
								addpointc($userdata,$auctiondata);
								$msg = 1;
							}
						}elseif ($userdata['priority'] == 2){    //优先使用虚拟币
							if ($userdata['pointb'] < $auctiondata['usebeat']){   //虚拟币数 < 竞标一次所需币数
								if ($userdata['beat'] < $auctiondata['usebeat']){   //如果实体币数 < 竞标一次所需币数  
									 //虚拟币数和实体币数均不够竞拍一次所需竞标币数
									$msg = 3; //余额不足
								}else {
									//执行实体币币竞拍操作
									usebeat($userdata,$auctiondata);
									addpointc($userdata,$auctiondata);  //增加积分
									$msg = 1;
								}	
							}else {  	//执行虚拟币币竞拍操作
								usepointb($userdata,$auctiondata);
								addpointc($userdata,$auctiondata);
								$msg = 1;
							}
						}
						
						
						
						
					}
				}
			}
		}
	}
	
	$json = array($msg,$auctiondata['usebeat']);
	echo json_encode($json);	
	
	
	
	
	
	
	
	
	
	
	
	
	

	/**
	 * 使用实体币
	 * Enter description here ...
	 * @param unknown_type $userdata
	 * @param unknown_type $auctiondata
	 */
	function usebeat($userdata,$auctiondata){
		global $db,$prefix,$cache,$get;

		//更新用户实体币数
		$sql = "UPDATE `{$prefix}user` SET `beat` = `beat` - {$auctiondata['usebeat']} WHERE `id` = {$userdata['id']}";
		$db->query($sql);
		
		$currentprice = $auctiondata['realprice'] + $auctiondata['addprice'];    //当前价格 = 竞标价格 + 竞标金额累增值
		
		unset($sql);
		
		$ip = get_client_ip();  //IP地址
		$address = getlocaltion($ip);  //根据IP地址获取地区
		$time = time();
		//插入竞拍历史表
		$sql = "INSERT INTO `{$prefix}auction_log` (`id`, `aid`, `uid`, `name`, `goodsname`, `beat`, `pointb`, `ip`, `address`, `bidprice`, `addtime`) 
				VALUES (NULL, '{$auctiondata['id']}', '{$userdata['id']}', '{$userdata['name']}', '{$auctiondata['goodsname']}', '{$auctiondata['usebeat']}'
				,'0', '{$ip}', '{$address}', '{$currentprice}', '{$time}')";
		
		$logdata = $get->getLogData($auctiondata['id']); //先获取缓存
		
		$db->query($sql); //再写进数据库
		
		$log_id = $db->insert_id();  //获取刚插入ID
		
		unset($sql);
		$sql = "SELECT * FROM `{$prefix}auction_log` WHERE `id` = {$log_id}";
		$tmp = $db->fetch_first($sql);  //读出刚写入信息
		
		$num = array_unshift($logdata,$tmp);     //然后对 缓存内容进行更新
		if ($num > 10){
			array_pop($logdata);
		}
		$key = 'history_'.$auctiondata['id'];
		$cache->set($key,$logdata);
		
		
		
		//对竞拍内容的更新
		if($auctiondata['maxaddsecond']>0){
			$diftime = $auctiondata['endtime']-$time;
			if($diftime < $auctiondata['maxaddsecond']){
				$currenttime = $auctiondata['endtime'] + $auctiondata['addsecond']; //当前时间
				if($currenttime-$time > $auctiondata['maxaddsecond'])$currenttime=$time+$auctiondata['maxaddsecond'];
			}else{
				$currenttime = $auctiondata['endtime'];
			}
		}else{
			$currenttime = $auctiondata['endtime'] + $auctiondata['addsecond']; //当前时间
		}
		
		
		//对数据库的竞拍数据更新
		$auctioncount = $auctiondata['auctioncount']+1;     //竞拍次数
		unset($sql);
		
		//更新商品竞拍属性
		$sql = "UPDATE `{$prefix}auction` SET `uid` = '{$userdata['id']}', `auctionuname` = '{$userdata['name']}', 
				`auctioncount` = '{$auctioncount}', `realprice` = '{$currentprice}', `endtime` = '{$currenttime}' 
				WHERE `id` = {$auctiondata['id']}";
		$db->query($sql); //写进数据库
		
		
		
		$sql = "SELECT * FROM `{$prefix}auction` where `id`={$auctiondata['id']}";
		$cachedata = $db->fetch_first($sql);
		$cachedata['head'] = $userdata['head'];
		$cachedata['uptime'] = microtime(true);
		
		$cache->set($auctiondata['id'],$cachedata);
	}
	
	
	
	
	
	/**
	 * 使用虚拟币
	 * Enter description here ...
	 * @param unknown_type $userdata
	 * @param unknown_type $auctiondata
	 */
	function usepointb($userdata,$auctiondata){
		global $db,$prefix,$cache,$get;
		
		//修改用户虚拟币数信息
		$sql = "UPDATE `{$prefix}user` SET `pointb` = `pointb` - {$auctiondata['usebeat']} WHERE `id` = {$userdata['id']}";
		$db->query($sql);
		
		$currentprice = $auctiondata['realprice'] + $auctiondata['addprice']; //当前价格 = 竞标金额 +竞标金额增加值
		
		
		unset($sql);
		
		//IP地址和地区
		$ip = get_client_ip();
		$address = getlocaltion($ip);
		$time = time();
		
		//记入竞标历史
		$sql = "INSERT INTO `{$prefix}auction_log` (`id`, `aid`, `uid`, `name`, `goodsname`, `beat`, `pointb`, `ip`, `address`, `bidprice`, `addtime`) 
				VALUES (NULL, '{$auctiondata['id']}', '{$userdata['id']}', '{$userdata['name']}', '{$auctiondata['goodsname']}', '0'
				,'{$auctiondata['usebeat']}', '{$ip}', '{$address}', '{$currentprice}', '{$time}')";
		
		$logdata = $get->getLogData($auctiondata['id']); //先获取缓存
		$db->query($sql); //再写进数据库
		
		$log_id = $db->insert_id(); //获取刚才写入的ID
		
		unset($sql);
		$sql = "SELECT * FROM `{$prefix}auction_log` WHERE `id` = {$log_id}";
		$tmp = $db->fetch_first($sql);//再把它读出来
		
		$num = array_unshift($logdata,$tmp);     //然后对 缓存内容进行更新
		if ($num > 10){
			array_pop($logdata);
		}
		$key = 'history_'.$auctiondata['id'];
		$cache->set($key,$logdata);
		
		
		//对竞拍内容的更新
		if($auctiondata['maxaddsecond']>0){
			$diftime = $auctiondata['endtime']-$time;
			if($diftime < $auctiondata['maxaddsecond']){
				$currenttime = $auctiondata['endtime'] + $auctiondata['addsecond']; //当前时间
				if($currenttime-$time > $auctiondata['maxaddsecond'])$currenttime=$time+$auctiondata['maxaddsecond'];
			}else{
				$currenttime = $auctiondata['endtime'];
			}
		}else{
			$currenttime = $auctiondata['endtime'] + $auctiondata['addsecond']; //当前时间
		}
		
		//对数据库的竞拍数据更新
		$auctioncount = $auctiondata['auctioncount']+1;     //竞标次数
		unset($sql);
		
		$sql = "UPDATE `{$prefix}auction` SET `uid` = '{$userdata['id']}', `auctionuname` = '{$userdata['name']}', 
				`auctioncount` = '{$auctioncount}', `realprice` = '{$currentprice}', `endtime` = '{$currenttime}' 
				WHERE `id` = {$auctiondata['id']}";
		$db->query($sql); //写进数据库
		
		
		$sql = "SELECT * FROM `{$prefix}auction` where `id`={$auctiondata['id']}";
		$cachedata = $db->fetch_first($sql);
		$cachedata['head'] = $userdata['head'];
		$cachedata['uptime'] = microtime(true);
		
		
		$cache->set($auctiondata['id'],$cachedata);
	}
	
	
	
    /**
     * 赠送积分
     * Enter description here ...
     * @param unknown_type $userdata
     * @param unknown_type $auctiondata
     */
	function addpointc($userdata,$auctiondata){
		//编辑用户积分
		if($auctiondata['pointc']>0){
			global $db,$prefix,$sysconfig;
			
			//同步用户积分信息
			$sql = "UPDATE `{$prefix}user` SET `pointc` = `pointc` + {$auctiondata['pointc']} WHERE `id` = {$userdata['id']}";
			$db->query($sql)    ;//赠送积分写进数据库
			unset($sql);
			
			//记录积分历史
			$payment="参加竞拍";
			$time = time();
			$remark="参加竞拍,{$auctiondata['goodsname']},点击1次,赠送{$auctiondata['pointc']}{$sysconfig['pointc_name']}";
			$sql = "INSERT INTO `{$prefix}pointclog` (`id`, `uid`, `name`, `payment`, `money`, `addtime`, `remark`) 
					VALUES (NULL, '{$userdata['id']}', '{$userdata['name']}', '{$payment}', '{$auctiondata['pointc']}', '{$time}','{$remark}')";
			$db->query($sql); //日志再写进数据库
			
			unset($sql);
		}
	}
	
	
	
	
	
?>